/**
 * Created by Administrator on 2018/4/28.
 */
/**
 * Created by Administrator on 2018/4/26.
 */
Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<this.length;i++){
        for(var j=i+1; j<this.length;j++){
            if(a[i] === a[j])
                a.splice(j, 1);
        }
    }
    return a;
};

define(function (require,exports,module) {
    var sTpl = require("./template.html");
    require("../../../lib/fullcalendar/moment.min.js");
    var DEFINE_STATUS = require("../../util/status");
    var message= require("../../messagePush/messagePush");
    require("./examine.css");
    var VueComponent = Vue.extend({
        template: sTpl,
        components:{
            'messages':message
        },
        methods:{
            queryAppointment :function(){
                 var  exceptDateS = this.form.startDate?this.form.startDate[0]:"";
                var  exceptDateE = this.form.startDate?this.form.startDate[1]:"";
                var  estimatedTimeS = this.form.endDate?this.form.endDate[0]:"";
                var  estimatedTimeE = this.form.endDate?this.form.endDate[1]:"";

                this.urls.url=this.urls.initurl+"&importOrExport=进港"+"&flightInfoId="+this.form.flightInfo.flightInfoId+
                    "&exceptDateS="+exceptDateS+"&exceptDateE="+exceptDateE+"&estimatedTimeS="+estimatedTimeS+"&estimatedTimeE="+estimatedTimeE;
                this.urls.url2=this.urls.initurl+"&importOrExport=出港"+"&flightInfoId="+this.form.flightInfo.flightInfoId+
                    "&exceptDateS="+exceptDateS+"&exceptDateE="+exceptDateE+"&estimatedTimeS="+estimatedTimeS+"&estimatedTimeE="+estimatedTimeE;
                var $this = this;
                axios.get(this.urls.url).then(function (response){
                    $this.importData=response.data;
                });
                axios.get(this.urls.url2).then(function (response){
                    $this.exportData=response.data;
                });
            },
            onSubmit:function() {
                console.log('submit!');
            },
            querySearchUnit:function(queryString, cb) {
                var restaurants = this.units;
                console.log(this.form);
                var results = queryString ? restaurants.filter(function(restaurant){
                    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                }) : restaurants;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },pickTime:function (val) {
                this.formData.appoDate = val;
            },
            openEnter:function (val) {
                if(!val){
                    if(this.preUrl==this.url){
                        this.$refs.table.freshTableList();
                    }
                }
                this.enterOpen=!val;
                var $this = this;


            },
             cellClassName:function (val1, val2) {
                   if(val1.column.label=="航班号"){
                       return "flight-num";
                   }
             },
            rowClass:function (row,a) {
                if(row.row.appoDate){
                    var today =  new Date(moment("").format("YYYY-MM-DD")).getTime();
                    var today =  new Date(moment("").format("YYYY-MM-DD")).getTime();
                    var rowDate =new Date(moment(row.row.appoDate).format("YYYY-MM-DD")).getTime() ;
                    if(today==rowDate){
                        return "today-plane";
                    }

                    if(rowDate==today+1000*3600*24*1){
                        return "next-day-plane";
                    }
                    if(rowDate>=today+1000*3600*24*2){
                        return "third-day-plane";
                    }
                    if(rowDate==today+1000*3600*24*3){
                        return "fourth-day-plane";
                    }
                    if(rowDate>=today+1000*3600*24*4){
                        return "firth-day-plane";
                    }
                    return "history";
                }
            },

            edit:function (scope) {
                this.openEnter(true);
                this.enterData.editOrAdd=='edit'
                var $this= this;

            },
            throughAudit:function(scope){
                var $this = this ;
                this.$confirm('确定要通过审核？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+'/api/appointment/throughAuditAppointmentAndFlight',scope.row)
                        .then(function (response) {

                            if($this.appointment.appProperty=="进港"){
                                $this.$set($this.importData,scope.$index,response.data)
                            }else{
                                $this.$set($this.exportData,scope.$index,response.data)
                            }
                            $this.appointmentEdit=false;
                            $this.$notify({
                                title: '审核成功',
                                message: '改预约已通过审核'
                            });
                        });
                })
                // var $this= this;
                // this.$refs.enters.formData=scope.row;
                // this.$refs.enters.parentData.showOrEdit='edit';
                // this.$refs.enters.parentData.dataCallBack=function(data){
                //     scope.row=data;
                // }
                // $("#panel-activer").click();
            },
            examineAppointment:function(scope){
                this.$refs.enters.formData=scope.row;
                this.$refs.enters.parentData.showOrEdit='show';
                this.$refs.enters.parentData.dataCallBack=function(data){
                    scope.row=data;
                }
                $("#panel-activer").click();
            },
            editFlightPlane:function(row){
                this.flightPlaneEdit=true;
                var $this=this;
                this.enterData.editOrAdd="edit";
                this.enterData.openEdit=function (a) {
                    a(row.flightPlane);
                }
                var $this =this;
                this.enterData.editCallback=function (val) {
                    $this.flightPlaneEdit=false;
                    $this.appointmentEdit=false;
                    $this.$refs.table.freshTableList();
                    $this.$refs.table2.freshTableList();
                }

            },
            editAppointment:function (scope) {
                this.appointmentEdit=true;
                this.appointment =  JSON.parse(JSON.stringify(scope.row));
                this.handleSelect(scope.row.flightInfo.flightInfoId);
                this.currentAppointmentScope = scope;
            },
                handleSelect:function (item) {
                    this.flightInfo =this.planes.find(function (val) {
                        return val.flightInfoId ==item;
                    })
                    console.log(item);
                },
                pickExpectDate:function (val) {
                if(val==null){
                    this.appointment.appoDate = "";
                }else{
                    this.appointment.appoDate = moment(val).format("YYYY-MM-DD");;
                }

                },
               pickEstimatedTime:function (val) {
                if(val==null){
                    this.appointment.estimatedTime = "";
                }else{
                    this.appointment.estimatedTime = moment(val).format("YYYY-MM-DD");;
                }

                },
            querySearchPlace:function(queryString, cb) {
                var restaurants = this.citys;
                var results = queryString ? restaurants.filter(function(restaurant){
                    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                }) : restaurants;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },
            confirmEditAppointment :function(){
                var $this = this ;
                this.$refs["appointment"].validate(function(valid) {
                    if (valid) {
                        var data = JSON.parse(JSON.stringify($this.appointment));
                        if(!data.agreement||!data.agreement.contractManageId){
                            data.agreement=null;
                        }
                        axios.post(basepath+'/api/appointment/saveAppointmentAndFlight',data)
                            .then(function (response) {
                                $this.appointmentEdit=false;
                                var index= -1;
                                $this.importData.forEach(function (val,i) {
                                    if(val.appoId==response.data.appoId){
                                        index = i;
                                    }
                                })
                                if(index!=-1){
                                    $this.$set($this.importData,index,response.data);
                                }
                                index = -1;
                                $this.exportData.forEach(function (val,i) {
                                    if(val.appoId==response.data.appoId){
                                        index = i;
                                    }
                                })
                                if(index!=-1){
                                    $this.$set($this.exportData,index,response.data);
                                }
                                $this.$notify({
                                    title: '修改成功',
                                    message: '修改成功'
                                });
                            });
                        return true;
                    } else {
                        $this.$notify({
                            title: '校验失败',
                            message: '请检查修改字段'
                        });
                    }
                });
            }
            },

        mounted:function() {
            var $this =this;
            this.queryAppointment();
            this.$refs.table.$watch("internalData",function(nv ,ov){

                $this.import=nv;
                $this.tableData =$this.import.concat($this.outPut).unique();

            })
            this.$refs.table2.$watch("internalData",function(nv ,ov){

                $this.outPut =nv;
                $this.tableData =$this.outPut.concat($this.import).unique();
            })

        }
        ,data:function(){
            return {
                DEFINE_STATUS:DEFINE_STATUS,
                flightPlaneEdit:false,
                appointmentEdit:false,
                currentAppointmentScope:{},
                diawidth:"750px",
                messages:{
                    messageType:"RESERVATION_AUDIT"
                },
                form: {

                    appoUnit: '',
                    flightInfo:{flightInfoId:""},
                    startDate:"",
                    endDate:'',
                    unExamine:false,
                    appoCancle:false,
                    examinePass:false,
                    examineCancle:false,
                    appoPhoneNum:'',
                    type:""
                },
                importData:[],
                exportData:[],
                enterData:{
                    editOrAdd:'add',
                    unShowExcel:"true"
                    ,onlyTime:true
                },
                enterOpen:true,
                tableData:[],
                import:[],
                outPut:[],
                units: [],
                urls:{
                    url:basepath+"/api/appointment/findAppointmentFlightPlane?sort=appoDate&status=审核通过&importOrExport=进港"+"&exceptDateS="+moment().format("YYYY-MM-DD")+"&exceptDateE="+moment(new Date().getTime() + 3600 * 1000 * 24 * 7).format("YYYY-MM-DD"),
                    url2:basepath+"/api/appointment/findAppointmentFlightPlane?sort=appoDate&status=审核通过&importOrExport=出港"+"&exceptDateS="+moment().format("YYYY-MM-DD")+"&exceptDateE="+moment(new Date().getTime() + 3600 * 1000 * 24 * 7).format("YYYY-MM-DD"),
                    initurl:basepath+"/api/appointment/findAppointmentFlightPlaneNoPage?sort=appoDate&status=审核通过",
                },
                preUrl:basepath+"/api/appointment/findAppointment?sort=appoId&status=审核通过",
                formData: [],
                appointment:{flightInfo:{flightInfoId:""},flightPlane:{}},
                flightInfo:{},
                planes:[],
                citys:[{value:'北京'},
                    {value:'天津'},
                    {value:'上海'},
                    {value:'徐州'},
                    {value:'常州 '},
                    {value:'江苏'},
                    {value:'南通'},
                    {value:'无锡'},
                    {value:'南京'},
                    {value:'连云港'},
                    {value:'济南'},
                    {value:'临沂'},
                    {value:'潍坊'},
                    {value:'威海'},
                    {value:'烟台'},
                    {value:'青岛'},
                    {value:'济宁'},
                    {value:'黄岩路'},
                    {value:'衢州'},
                    {value:'舟山'},
                    {value:'宁波'},
                    {value:'温州'},
                    {value:'杭州'},
                    {value:'锦州'},
                    {value:'朝阳'},
                    {value:'沈阳'},
                    {value:'大连'},
                    {value:'赤峰'},
                    {value:'内蒙古'},
                    {value:'锡林浩特'},
                    {value:'包头'},
                    {value:'呼和浩特'},
                    {value:'乌兰浩特'},
                    {value:'海拉尔'},
                    {value:'乌海'},
                    {value:'呼伦贝尔'},
                    {value:'通辽'},
                    {value:'南平市'},
                    {value:'厦门'},
                    {value:'泉州'},
                    {value:'晋江'},
                    {value:'福州'},
                    {value:'黄山屯溪'},
                    {value:'安庆'},
                    {value:'合肥'},
                    {value:'阜阳'},
                    {value:'蚌埠'},
                    {value:'嘉峪关'},
                    {value:'酒泉'},
                    {value:'庆阳'},
                    {value:'兰州'},
                    {value:'深圳'},
                    {value:'珠海'},
                    {value:'梅县'},
                    {value:'湛江'},
                    {value:'汕头'},
                    {value:'北海福城'},
                    {value:'广西'},
                    {value:'梧州'},
                    {value:'柳州'},
                    {value:'南宁'},
                    {value:'桂林'},
                    {value:'贵州'},
                    {value:'兴义'},
                    {value:'铜仁'},
                    {value:'黎平'},
                    {value:'龙洞堡'},
                    {value:'荔波'},
                    {value:'海口'},
                    {value:'三亚'},
                    {value:'秦皇岛'},
                    {value:'邯郸'},
                    {value:'邢台'},
                    {value:'石家庄'},
                    {value:'洛阳'},
                    {value:'南阳'},
                    {value:'郑州'},
                    {value:'黑河'},
                    {value:'佳木斯'},
                    {value:'哈尔滨'},
                    {value:'齐齐哈尔'},
                    {value:'牡丹江'},
                    {value:'武汉'},
                    {value:'宜昌'},
                    {value:'襄樊'},
                    {value:'恩施'},
                    {value:'常德'},
                    {value:'张家界'},
                    {value:'长沙'},
                    {value:'怀化'},
                    {value:'延吉'},
                    {value:'长白山'},
                    {value:'吉林'},
                    {value:'长春'},
                    {value:'德镇'},
                    {value:'井冈山'},
                    {value:'九江庐山'},
                    {value:'吉安'},
                    {value:'南昌'},
                    {value:'赣州'},
                    {value:'银川'},
                    {value:'西宁'},
                    {value:'格尔木'},
                    {value:'运城'},
                    {value:'太原'},
                    {value:'大同'},
                    {value:'长治'},
                    {value:'西安'},
                    {value:'安康'},
                    {value:'延安'},
                    {value:'榆林'},
                    {value:'汉中'},
                    {value:'成都'},
                    {value:'康定'},
                    {value:'达县'},
                    {value:'西昌'},
                    {value:'绵阳'},
                    {value:'四川攀枝花'},
                    {value:'九寨沟'},
                    {value:'广汉'},
                    {value:'南充'},
                    {value:'宜宾'},
                    {value:'林芝'},
                    {value:'昌都邦达'},
                    {value:'拉萨贡嘎'},
                    {value:'阿勒泰'},
                    {value:'伊宁'},
                    {value:'喀纳斯'},
                    {value:'喀什'},
                    {value:'库尔勒'},
                    {value:'克拉玛依'},
                    {value:'库车'},
                    {value:'且末'},
                    {value:'富蕴'},
                    {value:'和田'},
                    {value:'哈密'},
                    {value:'伊犁那拉提'},
                    {value:'鄯善'},
                    {value:'塔城'},
                    {value:'乌鲁木齐'},
                    {value:'阿克苏'},
                    {value:'景洪'},
                    {value:'昭通'},
                    {value:'文山普者黑'},
                    {value:'普洱'},
                    {value:'大理'},
                    {value:'丽江'},
                    {value:'云南迪庆香格里拉'},
                    {value:'德宏芒市'},
                    {value:'昆明巫家坝'},
                    {value:'重庆'},
                    {value:'香港'},
                    {value:'澳门'},
                    {value:'台北'},
                    {value:'桃园'},
                    {value:'台中'},
                    {value:'台东'},
                    {value:'台南'}
                ],
                pickerOptions2: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 7).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 30).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 90).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                }
            }
        },
        created:function (){
            var $this = this;
            axios.get(basepath+"/api/flightInfo/queryFlightInfo?filter=").then(function (response) {
                $this.planes=response.data;
                $this.planesMap={};
                $this.planes.forEach(function(plane){
                    plane.value= plane.flightNum;
                    $this.planesMap[plane.flightNum] = plane;
                })
            })
        },
        activated:function () {
            messages.$emit('change','预约审核');
        }
    });
    module.exports = VueComponent;
})
